package com.nova.android.tvlauncher.interfaces.factory

import com.nova.android.tvlauncher.interfaces.IMainManager
import java.util.ServiceLoader

object MainManagerFactory {
    val mainManager: IMainManager by lazy {
        // 使用应用类加载器
        val classLoader = MainManagerFactory::class.java.classLoader
        val loader = ServiceLoader.load(IMainManager::class.java, classLoader).toList()


        return@lazy when {
            loader.isEmpty() -> throw IllegalStateException("No provider found")
            loader.size == 1 -> loader.first()
            else -> {
                loader.firstOrNull { impl ->
                    !impl.javaClass.simpleName.contains("Default", ignoreCase = true)
                } ?: loader.first() // 如果全都带Default，就兜底返回第一个
            }
        }
    }
}
